<?php
/**
 * alltosun.com 核销平台控制器 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址:   http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 高昂 (gaoa@alltosun.com) $
 * $Date: 2014-11-17 下午6:41:46 $
 * $Id$
*/

class Action
{
    private $per_page = 10;
    /**
    * 添加核销平台
    */
    public function add()
    {
        Response::display('admin/add.html');
    }

    /**
    * 新建核销平台
    */
    public function create()
    {
        //接受表单提交的数据
        $info = Request::getParam('info', array());

        //创建两个表的数据
        //group_platform表
        $seller_info = array();
        $seller_info['group_platform_name']     = $info['group_platform_name'];
        $seller_info['group_platform_activity'] = $info['group_platform_activity'];
        $seller_info['add_time']                = date('Y-m-d H:i:s', time());
        $seller_info['status']                  = 1;
        $seller_info['update_time']             = date('Y-m-d H:i:s', time());
        $seller_info['is_open']                 = $info['is_open'];
		$seller_info['sort']                    = $info['sort']; 

        // 图片上传
        if ($_FILES['group_platform_pic']['error'] == 0) {
            try {
                $group_platform_pic_info = upload_file($_FILES['group_platform_pic'], false, 'group_platform');
                $seller_info['group_platform_pic'] = $group_platform_pic_info['file_path'];
            } catch(Exception $e) {
                return $e->getMessage();
            }
        }


        $group_platform_id = _model('group_platform')->create($seller_info);
        if ($group_platform_id) {

            //group_platform_detail表
            $seller_detail_info['group_platform_id'] = $group_platform_id;
            if ($info['intro']) {
                $seller_detail_info['intro']   = $info['intro'];
            } else {
                $seller_detail_info['intro']   = '';
            }
            if ($info['content']) {
                $seller_detail_info['content'] = $info['content'];
            } else {
                $seller_detail_info['content'] = '';
            }
            $res = _model('group_platform_detail')->create($seller_detail_info);
        }

        Response::redirect(AnUrl('group_platform/admin'));
    }

    /**
    * 显示核销平台列表
    */
    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::Get('status', 1);
        $is_advenced   = Request::Get('is_advenced', 0);
        $search_filter = Request::getParam('search_filter', array());

        //过滤条件
        $filter = array();
        $filter['status'] = $status;
        if (isset($search_filter['group_platform_name']) && $search_filter['group_platform_name']) {
            $filter[' `group_platform_name` LIKE '] = $search_filter['group_platform_name'].'%';
        }
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'].' 00:00:00';
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'].' 23:59:59';
        }

        $group_platform_list  = array();
        
        $group_platform_list = get_data_list('group_platform', $filter, ' ORDER BY `id` DESC ', $page, $per_page = 20);
        if ($group_platform_list) {
            foreach ($group_platform_list as $k => $v) {
                $group_platform_list[$k]['group_platform_pic'] = _image($v['group_platform_pic'], 'list', 'group_platform');
            }
        }
        
        Response::assign('group_platform_list', $group_platform_list);
        Response::assign('status', $status);
        Response::assign('is_advenced', $is_advenced);
        Response::assign('search_filter', $search_filter);
        Response::display('admin/group_platform_list.html');
    }

    /**
    * 更新核销平台信息
    */
    public function edit()
    {
        //接受参数
        $id = Request::Get('id', 1);
        //获取单条信息
        //获取group_platform表的信息
        $seller_info = _uri('group_platform', $id);
        //获取group_platform_detail表的信息
        $filter['group_platform_id'] = $id;
        $seller_detail_info = _model('group_platform_detail')->read($filter);

        //合并数组
        $info = array_merge($seller_info, $seller_detail_info);

        Response::assign('info', $info);
        Response::display('admin/edit.html');
    }

    /**
    * 执行更新
    */
    public function save()
    {
        //接收
        $info = Request::getParam('info', array());
        // 创建两个表的数据
        // 创建group_platform表的数据
        $seller_info['id']                      = $info['id'];
        $seller_info['group_platform_name']     = $info['group_platform_name'];
        $seller_info['update_time']             = date('Y-m-d H:i:s', time());
        $seller_info['group_platform_activity'] = $info['group_platform_activity'];
        $seller_info['is_open']                 = $info['is_open'];
        $seller_info['sort']                    = $info['sort'];
		
        //数值过滤
        if (!is_int($seller_info['sort'])) {
        	$sell_info['sort'] = 1;
        }
        
        // 图片上传
        if($_FILES['group_platform_pic']['error'] == 0){
            try {
                $group_platform_pic_info = upload_file($_FILES['group_platform_pic'], false, 'group_platform');
                $seller_info['group_platform_pic'] = $group_platform_pic_info['file_path'];
            } catch(Exception $e) {
                return $e->getMessage();
            }
        }
        
        _model('group_platform')->update($info['id'], $seller_info);

        //创建group_platform_detail表的数据
        $seller_detail_info['group_platform_id'] = $info['id'];
        $seller_detail_info['intro']             = $info['intro'];
        $seller_detail_info['content']           = $info['content'];

        _model('group_platform_detail')->update($info['id'],$seller_detail_info);
        
        Response::redirect(AnUrl('group_platform/admin'));
    }

    /**
    * 伪删除(禁用)
    */
    public function delete()
    {
        //接收id
        $id  = Request::getParam('id');

        $ids = explode(',', $id);
        foreach ($ids as $k => $v) {
            _model('group_platform')->update($v, array('status' => 0));
        }
        return 'ok';
    }

    /**
    * 恢复
    */
    public function change_status()
    {
        //接收id
        $id = Request::Get('id', 1);

        _model('group_platform')->update($id, array('status' => 1));

        Response::assign('status', 1);
        Response::redirect(AnUrl('group_platform/admin?status=1'));
    }

    /**
    * 删除
    */
    public function delete_group_platform()
    {
        //接收id
        $id = Request::getParam('id');

        $ids = explode(',', $id);
        foreach ($ids as $k => $v) {
            if(!$v){
                continue;
            }
            $group_platform_info = _uri('group_platform', intval($v));
            if(!$group_platform_info){
                continue;
            }
            _model('group_platform')->delete($v);
            _model('group_platform_detail')->delete(array('group_platform_id' => $v));
        }

        return 'ok';
    }

}

?>